package Conference;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @ClassName: Conf2
 * @Auther: Drawin
 * @Date: 2021/12/24 15:51
 * @Description:
 */

public class Conf2 extends JFrame{

        //存放计算结果的文本框
        private JTextField txtResult;
        private JPanel p;
        private String name[] = {"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"};
        //声明一个按钮数组，以字符串数组的长度为准
        private JButton button[] = new JButton[name.length];
        private double result = 0;
        //存放最后一个操作符
        private String lastCommand = "=";
        //标识是否为开始
        private boolean start = true;
        public Conf2() {
            super("计算器");
            //实例化文本框控件
            txtResult = new JTextField(20);
            //设置文本框不是焦点状态
            txtResult.setFocusable(false);
            //将文本框放在窗口的北方
            this.add(txtResult, BorderLayout.NORTH);
            //设置面板布局4*4的网格布局
            p = new JPanel(new GridLayout(4,4));
            //用循环的方法实例化按钮对象数组
            //实例化按钮监听对象
            ButtonAction ba = new ButtonAction();
            //实例化键盘监听对象
            KeyAction ka = new KeyAction();
            //循环实现
            for (int i=0;i<button.length;i++) {
                button[i] = new JButton(name[i]);
                //注册监听
                button[i].addActionListener(ba);
                button[i].addKeyListener(ka);
                p.add(button[i]);
            }
            this.add(p,BorderLayout.CENTER);
            this.setSize(200,150);
            this.setLocation(100,100);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        }
        //计算
        public void calculate(double x) {
            if(lastCommand.equals("+"))
                result+=x;
            else if(lastCommand.equals("-"))
                result-=x;
            else if(lastCommand.equals("*"))
                result*=x;
            else if(lastCommand.equals("/"))
                result/=x;
            else if(lastCommand.equals("="))
                result=x;
            //将结果显示在文本框
            txtResult.setText(""+result);
        }
        //单击按钮监听
        private class ButtonAction implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                String input = e.getActionCommand();
                if(input.equals("+")||input.equals("-")||input.equals("*")
                        ||input.equals("/")||input.equals("=")) {
                    if(start) {
                        if(input.equals("-")) {
                            txtResult.setText(input);
                            start = false;
                        }else
                            lastCommand=input;
                    }else {
                        calculate(Double.parseDouble(txtResult.getText()));
                        lastCommand=input;
                        start=true;
                    }
                }else {
                    if(start) {
                        txtResult.setText("");
                        start=false;
                    }
                    txtResult.setText(txtResult.getText()+input);
                }
            }
        }
        //键盘监听
        private  class KeyAction extends KeyAdapter {
            public void keyTyped(KeyEvent e) {
                char key = e.getKeyChar();
                if(key=='0'||key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9') {
                    if(start) {
                        txtResult.setText("");
                        start=false;
                    }
                    txtResult.setText(txtResult.getText()+key);
                }
                else if(key=='+'||key=='-'||key=='/'||key=='*') {
                    if(start) {
                        if(key=='-') {
                            txtResult.setText(String.valueOf(key));
                            start=false;
                        }else
                            lastCommand=String.valueOf(key);

                    }else {
                        calculate(Double.parseDouble(txtResult.getText()));
                        lastCommand=String.valueOf(key);
                        start=true;
                    }


                }
            }

        }
        public static void main(String[] args) {
            Conf2 f = new Conf2();
            f.setVisible(true);
        }
    }


